//
//  GradientLabelVC.swift
//  SwiftLearning
//
//  Created by 黄龙 on 2024/1/9.
//

import UIKit

class GradientLabelVC: BasicNavibarVC {
    var animateGradientLayer:CAGradientLayer?
    var animateLabelGradientLayer:CAGradientLayer?
    var mShineLabel:TQQShineLabel?
    
    var currentIndex = 0
    let animateColors = [
        [UIColor.systemBackground.cgColor,UIColor.yellow.cgColor,UIColor.orange.cgColor],
        [UIColor.systemBackground.cgColor,UIColor.cyan.cgColor,UIColor.green.cgColor],
        [UIColor.systemBackground.cgColor,UIColor.magenta.cgColor,UIColor.blue.cgColor]
    ]
    let shineTitle:[String] = ["人生如寄,多忧何为。今我不乐,岁月如驰。汤汤川流,中有行舟。随波转薄,有似客游。策我良马，被我轻裘。载驰载驱，聊以忘忧。",
                               "一别都门三改火，天涯踏尽红尘。依然一笑作春温。无波真古井，有节是秋筠。\n惆怅孤帆连夜发，送行淡月微云。尊前不用翠眉颦。人生如逆旅，我亦是行人。",
    "Genius only means hard-working all one’s life.\nFailure is the mother of success. \nDifficult circumstances serve as a textbook of life for people. "]
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //如果viewDidLoad里一句代码都没有，push显示后，会发现右滑不能返回上一层
    }
    
    override func initView(){
        animateGradientLayer = workView.gradientLayer(With: animateColors[currentIndex],locationSet: [0.1,0.6,1.0])

        let title = "中国万岁，中国人民万岁！"
        let label = UILabel(frame: CGRect(x: 0, y: 30, width: screen_W, height: 30))
        workView.addSubview(label)
        label.text = title
        label.textAlignment = .center
        label.font = .systemFont(ofSize: 24)
        //文字阴影
        label.addTextShadow(title: title, color: .cyan,offset: CGSize(width: -3, height: 5))
        //字体渐变色，方法1
        label.textColor = UIColor(patternImage: label.getGradientImage(colors: [UIColor.yellow.cgColor,UIColor.cyan.cgColor]))
        
        //字体渐变色，方法2
        let containerView = UIView.init(frame: CGRect.init(x: 0, y: 80, width: screen_W, height: 30))
        workView.addSubview(containerView)
        let label_2 = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: screen_W, height: 30))
        label_2.text = "LOVE CHINA,LONG LIVE CHINA"
        label_2.textAlignment = .center
        label_2.font = UIFont.boldSystemFont(ofSize: 24)
        containerView.addSubview(label_2)
        if let gradientLayer = containerView.gradientLayer(With: [UIColor.red.cgColor, UIColor.green.cgColor],startPoint: CGPoint(x: 0, y: 0.5),endPoint: CGPoint(x: 1, y: 0.5)){
            gradientLayer.frame = label_2.frame
            gradientLayer.mask = label_2.layer
        }
        
        let animateView = UIView.init(frame: CGRect.init(x: 0, y: 130, width: screen_W, height: 30))
        workView.addSubview(animateView)
        let label_3 = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: screen_W, height: 30))
        label_3.text = "ANIMATE CHINA,LONG LIVE CHINA"
        label_3.textAlignment = .center
        label_3.font = UIFont.boldSystemFont(ofSize: 24)
        animateView.addSubview(label_3)
        animateLabelGradientLayer = animateView.gradientLayer(With: [UIColor.systemBackground.cgColor, UIColor.label.cgColor,UIColor.systemBackground.cgColor],locationSet: [0,1],startPoint: CGPoint(x: 0, y: 0.5),endPoint: CGPoint(x: 1, y: 0.5))
        if let gradientLayer = animateLabelGradientLayer{
            gradientLayer.frame = label_3.frame
            gradientLayer.mask = label_3.layer
            
            let gradientAnimation = CABasicAnimation(keyPath: "locations") //通过更改locations的值，形成投光动画效果
            gradientAnimation.fromValue = [0, 0, 0.2]
            gradientAnimation.toValue = [0.8, 1, 1]
            gradientAnimation.duration = 12.5
            //动画一致重复执行
            gradientAnimation.repeatCount = MAXFLOAT
            gradientAnimation.isRemovedOnCompletion = false //没有此句切到后台动画会停止
            gradientLayer.add(gradientAnimation, forKey: "locationsAnimate")
        }
        
        let horseView = TQQHorseCycleView(frame: CGRect(x: 15, y: 180, width: screen_W-30, height: 30), title: "人生如寄 多忧何为 今我不乐 岁月如驰 汤汤川流 中有行舟 随波转薄 有似客游")
        workView.addSubview(horseView)
        horseView.backgroundColor = .systemBackground.withAlphaComponent(0.1)
        horseView.horseRun(12.0)
        
        let imgView = UIImageView(frame: CGRect(x: 15, y: 230, width: screen_W-30, height: 300))
        workView.addSubview(imgView)
        mShineLabel = TQQShineLabel(frame: CGRect(x: 15, y: 15, width: imgView.bounds.width-30, height: imgView.bounds.height-30), title: shineTitle[0])
        if let shineLabel = mShineLabel{
            imgView.addSubview(shineLabel)
        }
        
        self.rightBar.title = "切换"
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if let shineLabel = mShineLabel{
            shineLabel.shine()
        }
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        if let gradientLayer = animateGradientLayer{
            gradientLayer.removeAllAnimations()
        }
        
        if let gradientLayer = animateLabelGradientLayer{
            gradientLayer.removeAllAnimations()
        }
    }
    
    override func clickRightbarItem() {
        //添加渐变背景切换动画
        if let gradientLayer = animateGradientLayer{
            
            let colorChangeAnimation = CABasicAnimation(keyPath: "colors")
            colorChangeAnimation.delegate = self
            colorChangeAnimation.duration = 2.0
            colorChangeAnimation.fromValue = animateColors[currentIndex]
            currentIndex += 1
            if currentIndex>=animateColors.count{
                currentIndex = 0
            }
            colorChangeAnimation.toValue = animateColors[currentIndex]
            colorChangeAnimation.fillMode = .forwards
            //动画结束后保持最终的效果
            colorChangeAnimation.isRemovedOnCompletion = false
            gradientLayer.add(colorChangeAnimation, forKey: "colorChange")
            
            if let shineLabel = mShineLabel{
                if (shineLabel.isVisible()) {
                    shineLabel.fadeOutWithCompletion {[weak self] in
                        if let self = self{
                            shineLabel.text = self.shineTitle[self.currentIndex]
                            shineLabel.shine()
                        }
                    }
                }else{
                    shineLabel.shine()
                }
            }
        }
    }
    
    

}

extension GradientLabelVC:CAAnimationDelegate{
    func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
        print("animationDidStop: "+String(currentIndex))
    }
}
